package com.jh3502.vivo.function.contact;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseFragment;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;
import com.jh3502.vivo.view.treeview.BranchNode;
import com.jh3502.vivo.view.treeview.LayoutItem;
import com.jh3502.vivo.view.treeview.RootNode;
import com.jh3502.vivo.view.treeview.TreeNode;
import com.jh3502.vivo.view.treeview.TreeViewAdapter;
import com.jh3502.vivo.view.treeview.TreeViewBinder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author:harisucici on 2018/11/7 10:19
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class ContactFragment extends BaseFragment<ContactPresenter.ContactView, ContactPresenter> implements ContactPresenter.ContactView {
    @BindView(R.id.contact_recycle)
    MyRecyclerView contactRecycle;
    @BindView(R.id.contact_cancel)
    TextView contactCancel;
    @BindView(R.id.contact_submit)
    TextView contactSubmit;

    private List<TreeNode> dataAll = new ArrayList<>();
    private List<ContactBean> dataChosen = new ArrayList<>();
    private TreeViewAdapter adapter;
    private boolean hidebutton = false;
    private boolean singlechosen = false;
    private Bundle bundle;

    @Override
    public ContactPresenter initPresenter() {
        return new ContactPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.fragment_contact;
    }

    @Override
    protected void initView() {
        mPresenter.setContactView(this);
        bundle = getArguments();
        if (bundle == null) {
            return;
        }
        hidebutton = bundle.getBoolean("hidebutton");
        if (hidebutton) {
            contactCancel.setVisibility(View.GONE);
            contactSubmit.setVisibility(View.GONE);
        }
        singlechosen = bundle.getBoolean("singlechosen");


    }

    @Override
    protected void initData() {
        boolean weituo = bundle.getBoolean("weituo");
        if(weituo){
            mPresenter.toFindEntrustUsers();
        }else{
            Map<String, Object> body = new HashMap<>(2);
            body.put("name", "");
            mPresenter.toGetorganization(body);
        }




    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnVisible() {

    }

    @Override
    protected void doOnInvisible() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);

    }

    @Override
    public void requestFailure(Throwable t) {

    }

    @Override
    public void getorganizationSuccess(String s) {
        ArrayList<ContactBean> list = mPresenter.getContact(s);
        TreeSet<Integer> pidSet = new TreeSet<>();
        for (ContactBean contactBean : list) {
            pidSet.add(contactBean.getDeptId());
        }

        List<List<ContactBean>> contactList = new ArrayList<>(2);
        for (int m : pidSet) {
            List<ContactBean> persons = new ArrayList<>(2);
            for (ContactBean contactBean : list) {
                if (contactBean.getDeptId() == m) {
                    persons.add(contactBean);
                }
            }
            contactList.add(persons);
        }

        for (List<ContactBean> contactBeans : contactList) {
            TreeNode<RootNode> rootNode =
                    new TreeNode<>(new RootNode(contactBeans.get(0)));
            for (ContactBean contactBean : contactBeans) {
                TreeNode<BranchNode> branchNode = new TreeNode<>(new BranchNode(contactBean));
                rootNode.addChild(branchNode);
            }
            dataAll.add(rootNode);

        }
        initAdapter(hidebutton);
    }

    private void initAdapter(boolean b) {
        adapter = new TreeViewAdapter(dataAll,
                Arrays.asList(new RootViewBinder(), new BranchViewBinder(b))) {
            @Override
            public void toggleClick(TreeViewBinder.ViewHolder viewHolder, View view, boolean isOpen, TreeNode treeNode) {
                adapter.lastToggleClickToggle();
            }

            @Override
            public void toggled(TreeViewBinder.ViewHolder viewHolder, View view, boolean isOpen, TreeNode treeNode) {
                viewHolder.findViewById(R.id.root_node).setRotation(isOpen ? 180 : 0);
            }

            @Override
            public void checked(TreeViewBinder.ViewHolder viewHolder, View view, boolean checked, TreeNode treeNode) {
                ContactBean bean = (ContactBean) ((BranchNode) treeNode.getValue()).getBean();
                if (singlechosen) {
                    dataChosen.clear();
                    dataChosen.add(bean);
                } else {
                    if (checked) {
                        dataChosen.add(bean);
                    } else {
                        dataChosen.remove(bean);
                    }
                }
            }

            @Override
            public void itemClick(TreeViewBinder.ViewHolder viewHolder, View view, TreeNode treeNode) {
                LayoutItem item = treeNode.getValue();
                if (item instanceof RootNode) {
                    viewHolder.findViewById(R.id.root_node).performClick();
                } else if (item instanceof BranchNode) {
                    ContactBean contactBean = (ContactBean) ((BranchNode) item).getBean();
                    Bundle bundle = new Bundle();
                    bundle.putString("dep", contactBean.getDeptName());
                    bundle.putString("name", contactBean.getName());
                    bundle.putString("postname", contactBean.getPostName());
                    bundle.putString("telephone", contactBean.getTelephone());
                    bundle.putString("mobile", contactBean.getMobile());
                    Utils.getInstance().startActivity(mActivity, ContactDetailActivity.class, bundle);
                }

            }
        };
        adapter.setingleChosen(singlechosen);
        contactRecycle.setLayoutManager(new LinearLayoutManager(mContext));
        contactRecycle.setAdapter(adapter);
    }




    @OnClick(R.id.contact_cancel)
    public void onContactCancelClicked() {
        mActivity.onBackPressed();
    }

    @OnClick(R.id.contact_submit)
    public void onContactSubmitClicked() {
        Intent intent = new Intent();
        intent.putExtra("datachosen", new Gson().toJson(dataChosen));
        mActivity.setResult(Activity.RESULT_OK, intent);
        mActivity.onBackPressed();
    }

}
